package com.raedev.forms.validator

import com.raedev.forms.R
import com.raedev.forms.items.FormItem
import com.uduew.utils.ActivityUtils

class RequiredFormValidator(private val validator: FormValidator? = null) : FormValidator() {

    override fun validate(item: FormItem, value: String?): FormValidationResult {
        val text = value?.trim()
        if (text.isNullOrEmpty()) return error(
            item,
            ActivityUtils.getTopActivity().getString(R.string.it_cannot_be_empty, item.label)
        )

        // 装饰者模式
        val result = validator?.validate(item, value)
        if (result?.successfully() == false) return result

        return success(item)
    }
}